#ifndef RIGIDBODY_H
#define RIGIDBODY_H

#include "includes.h"
#include "AABB.h"
#include "GameStates.h"

#define  GS GameStates::getInstance()

// kh mini physics engine : D

typedef struct _state{
    vector3<float> position, velocity;
    _state() : position(0,0,0), velocity(0,0,0){}
} state;

typedef struct _deriv{
    vector3<float> velocity, accel;
    _deriv() :velocity(0,0,0), accel(0,0,0){}
} deriv;

struct RigidBody{

    AABB geometry;
    float mass;
    state s;
    deriv d;
    bool isDynamic, isCollidable;

    RigidBody(float _mass,
              AABB geo,
              bool dynamicObj=true,
              bool collidableObj=true)
              :geometry(geo),mass(_mass),
               isDynamic(dynamicObj),isCollidable(collidableObj){}

    void translate(vector3<float> v){
    s.position+=v;
    geometry.translate(v);
    }

    void setPosition(vector3<float> v){
        translate(v-s.position);
    }

      void calcDeriv(deriv& d, state& s){
          d.velocity = s.velocity;
          d.accel = -d.velocity * GS->getViscousDrag();
          d.accel.y += mass * -GS->getGravity();
          d.accel /= mass;
      }

      void update(){
          calcDeriv(d,s);
          s.position += d.velocity * TIMESTEP;
          geometry.translate(d.velocity * TIMESTEP);
          s.velocity += d.accel * TIMESTEP;
      }

   friend class PhysicsEngine;
};

#endif // RIGIDBODY_H
